import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/a_elder/models/code_value_entity.dart';
import 'package:ls_work/model/entry_record/entry_model.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/response/entry_record_list_data.dart';
import 'package:ls_work/viewmodel/model/combo_data_model.dart';
import 'package:saas_order_meal/model/combo_model.dart';

class EntryRecordListViewModel extends BaseListViewModel<EntryRecordListData> {
  EntryRecordListViewModel(super.context);

  var model = EntryRecordModel();

  var comboModel = ComboDataModel();

  List<CodeValue> customerListType = [];

  CodeValue? accessHumanType;

  List<PickerDataWithIdModel> entryType = [
    PickerDataWithIdModel(pickName: '进入', pickId: '0'),
    PickerDataWithIdModel(pickName: '离开', pickId: '1'),
  ];

  PickerDataWithIdModel? accessType;

  ///来访人姓名
  String? searchName;

  List<PickerDataWithIdModel> equipmentList = [];

  PickerDataWithIdModel? equipment;

  List<PickerDataWithIdModel> gatePointList = [];

  PickerDataWithIdModel? gatePoint;

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  @override
  Future<List<EntryRecordListData>> requestListData() async {
    if (customerListType.isEmpty) {
      customerListType = await comboModel.getComboData("209");
    }
    if (equipmentList.isEmpty) {
      equipmentList = await model.equipmentList();
    }

    if(gatePointList.isEmpty){
      gatePointList = await model.gatePointList();
    }
    var list = await model.entryRecordList(
        page: page,
        rows: pageLength,
        accessType: accessType?.pickId,
        accessHumanType: accessHumanType?.codeValue,
        accessHumanName: searchName,
        visitDateStart: startDateStr,
        visitDateEnd: endDateStr,
        iotId: equipment?.pickId);

    return list;
  }

  ///选择进出类型
  void chooseAccessType() {
    DataPickerView(context, entryType, currentFirstData: accessType,
        onSubmitListener: (p1, p2, p3) {
      accessType = entryType[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  ///客户类型
  void chooseClientType() {
    DataPickerView(context, customerListType, currentFirstData: accessHumanType,
        onSubmitListener: (p1, p2, p3) {
      accessHumanType = customerListType[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  ///选择设备
  void chooseEquipment() {
    DataPickerView(context, equipmentList, currentFirstData: equipment,
        onSubmitListener: (p1, p2, p3) {
      equipment = equipmentList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  ///选择门点
  void chooseGatePoint(){
    DataPickerView(context, gatePointList, currentFirstData: gatePoint,
        onSubmitListener: (p1, p2, p3) {
          gatePoint = gatePointList[p1];
          notifyListeners();
          requestRefresh();
        }).show();
  }


  void resetClick() {
    searchName = null;
    accessHumanType = null;
    gatePoint = null;
    accessType = null;
    equipment = null;
    gatePoint = null;
    startDateStr = "";
    startDateTime = null;
    endDateStr = "";
    endDateTime = null;
    notifyListeners();
    requestRefresh();
  }
}
